package org.garbagecan.xstreamstudy.sample1;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.thoughtworks.xstream.XStream;

public class Test {
	public static void main(String[] args) throws IOException {
		String file = Test.class.getResource("/org/garbagecan/xstreamstudy/sample1/test.xml").getFile();
		parse(file);
	}
	
	public static void parse(String file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("startcenter", StartCenter.class);
		xstream.useAttributeFor(StartCenter.class, "description");
		xstream.useAttributeFor(StartCenter.class, "layout");
		
		xstream.aliasAttribute(StartCenter.class, "bulletinBoard", "bulletinboard");
		xstream.aliasAttribute(BulletinBoard.class, "chartType", "charttype");
		xstream.aliasAttribute(BulletinBoard.class, "columnNum", "columnnum");
		xstream.aliasAttribute(BulletinBoard.class, "description", "description");
		xstream.aliasAttribute(BulletinBoard.class, "minOrMax", "minormax");
		xstream.aliasAttribute(BulletinBoard.class, "orderNum", "ordernum");
		xstream.aliasAttribute(BulletinBoard.class, "portletId", "portletid");
		xstream.aliasAttribute(BulletinBoard.class, "rowsToDisplay", "rowstodisplay");
		xstream.aliasAttribute(BulletinBoard.class, "showChart", "showchart");
		
		xstream.alias("resultset", ResultSet.class);
		xstream.addImplicitCollection(StartCenter.class, "resultSets", ResultSet.class);
		xstream.aliasAttribute(ResultSet.class, "chartType", "charttype");
		xstream.aliasAttribute(ResultSet.class, "columnNum", "columnnum");
		xstream.aliasAttribute(ResultSet.class, "description", "description");
		xstream.aliasAttribute(ResultSet.class, "minOrMax", "minormax");
		xstream.aliasAttribute(ResultSet.class, "orderNum", "ordernum");
		xstream.aliasAttribute(ResultSet.class, "portletId", "portletid");
		xstream.aliasAttribute(ResultSet.class, "rowsToDisplay", "rowstodisplay");
		xstream.aliasAttribute(ResultSet.class, "showChart", "showchart");

		xstream.alias("attribute", Attribute.class);
		xstream.addImplicitCollection(ResultSet.class, "attributes", Attribute.class);
		xstream.aliasAttribute(Attribute.class, "app", "app");
		xstream.aliasAttribute(Attribute.class, "columnOrder", "columnorder");
		xstream.aliasAttribute(Attribute.class, "description", "description");
		xstream.aliasAttribute(Attribute.class, "display", "display");
		xstream.aliasAttribute(Attribute.class, "isConditionSet", "isconditionset");
		xstream.aliasAttribute(Attribute.class, "isGraphon", "isgraphon");
		xstream.aliasAttribute(Attribute.class, "queryColumn", "querycolumn");
		xstream.aliasAttribute(Attribute.class, "queryId", "queryid");

		xstream.alias("portletdisplay", PortletDisplay.class);
		xstream.addImplicitCollection(ResultSet.class, "portletDisplays", PortletDisplay.class);
		xstream.aliasAttribute(PortletDisplay.class, "color", "color");
		xstream.aliasAttribute(PortletDisplay.class, "expression", "expression");
		xstream.aliasAttribute(PortletDisplay.class, "expvalue", "expvalue");

		xstream.aliasAttribute(StartCenter.class, "favoriteApplications", "favoriteapplications");
		xstream.aliasAttribute(FavoriteApplications.class, "chartType", "charttype");
		xstream.aliasAttribute(FavoriteApplications.class, "columnNum", "columnnum");
		xstream.aliasAttribute(FavoriteApplications.class, "description", "description");
		xstream.aliasAttribute(FavoriteApplications.class, "minOrMax", "minormax");
		xstream.aliasAttribute(FavoriteApplications.class, "orderNum", "ordernum");
		xstream.aliasAttribute(FavoriteApplications.class, "portletId", "portletid");
		xstream.aliasAttribute(FavoriteApplications.class, "rowsToDisplay", "rowstodisplay");
		xstream.aliasAttribute(FavoriteApplications.class, "showChart", "showchart");
		
		xstream.alias("application", Application.class);
		xstream.addImplicitCollection(FavoriteApplications.class, "applications", Application.class);
		xstream.aliasAttribute(Application.class, "app", "app");
		xstream.aliasAttribute(Application.class, "orderNum", "ordernum");
		
		InputStream is = new FileInputStream(file);
		StartCenter startCenter = (StartCenter)xstream.fromXML(is);
		is.close();
		
		System.out.println(startCenter);
		System.out.println(xstream.toXML(startCenter));
	}
}
